package com.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.dao.UserInfoMapper;
import com.web.domain.Response;
import com.web.entity.ExpertsInfo;
import com.web.entity.UserInfo;
import com.web.entity.UserQuiz;
import com.web.service.AskQuestionsService;
import com.web.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 提问题
 */
@Slf4j
@RestController
@RequestMapping(value = "/app")
public class AskQuestionsController {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AskQuestionsService askQuestionsService;


    /**
     * 专家
     * @param info
     * @return
     */
    @RequestMapping(value = "/AskQuestions/getExpertsList",method = RequestMethod.POST)
    public Response ExpertsList(@CurrentUser UserInfo info,Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        PageHelper.startPage(pageNum,10);
        List<ExpertsInfo> list = askQuestionsService.getExpertsList();
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }


    /**
     * 提问题
     * @param info
     * @return
     */
    @RequestMapping(value = "/AskQuestions/addAskQuestions",method = RequestMethod.POST)
    public Response addAskQuestions(@CurrentUser UserInfo info,UserQuiz userQuiz,String end_times){

        UserInfo userInfo = userInfoMapper.findById("user_id",info.getUserId());
        if(userInfo.getCurrency() < (userQuiz.getReward_amount()*2)){
            return Response.custom().failure("账户剩余模豆不足支付！请充值");
        }

        System.out.println("send_time : " + userQuiz.getEnd_time());

        userQuiz.setUser_id(userInfo.getUserId());
        userQuiz.setExpert_id(userQuiz.getExpert_id());
        userQuiz.setReward_amount(userQuiz.getReward_amount());
        userQuiz.setVideos(userQuiz.getVideos());
        userQuiz.setFirst_video_photos(userQuiz.getFirst_video_photos());
        userQuiz.setEnd_time(DateUtil.dateFormat(Long.valueOf(end_times)));
        userQuiz.setTitle(userQuiz.getTitle());
        userQuiz.setQuiz(userQuiz.getQuiz());
        userQuiz.setVideos(userQuiz.getVideos());
        userQuiz.setFirst_video_photos(userQuiz.getFirst_video_photos());
        userQuiz.setPhotos(userQuiz.getPhotos());
        userQuiz.setCreate_time(new Date());
        userQuiz.setModify_time(new Date());
        askQuestionsService.insertSelective(userQuiz);

        return Response.custom().ok();
    }


}
